const fs = require('fs');
const tar = require('tar');

const pack = new tar.Pack({ gzip: true });
const gzipPath = 'files';
//遍历文件夹，并以指定基准输出
readDirSync(gzipPath, "base");
function readDirSync(path, base) {
    if (base == undefined) {
        base = path
    }
    const pa = fs.readdirSync(path);
    pa.forEach((ele, index) => {
        var info = fs.statSync(path + "/" + ele);
        if (info.isDirectory()) {
            readDirSync(path + "/" + ele, base + "/" + ele);
        } else {
            pack.add(path + "/" + ele);
        }
    });
}

pack.end();
const writeStream = fs.createWriteStream('gzipData.tar.gz');
pack.pipe(writeStream);